
package com.dream.dto;

import com.dream.common.constans.ErrorCode;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
public class DataResponse<T> implements Serializable {
	public static final Integer SUCCESS = 0;
	public static final Integer FAIL = 1;

	private int code = 0;

	private String message = "success";

	private T data;

	@ApiModelProperty(value = "系统的时间戳**用于矫正手机本地时间不准的问题")
	public long sysTime;

	public DataResponse() {
		sysTime = System.currentTimeMillis();
	}

	public DataResponse(ErrorCode errorCode) {
		this.code = errorCode.code;
		this.message = errorCode.msg;
		this.sysTime = System.currentTimeMillis();
	}

	public DataResponse(ErrorCode errorCode, T data) {
		this.code = errorCode.code;
		this.message = errorCode.msg;
		this.data = data;
		this.sysTime = System.currentTimeMillis();
	}

	public DataResponse(T data) {
		this.data = data;
		this.sysTime = System.currentTimeMillis();
	}


	public static <T> DataResponse builderSuccess(T data) {
		DataResponse<T> dataResponse = new DataResponse<>();
		dataResponse.setData(data);
		return dataResponse;
	}


	public static DataResponse builderFailed(ErrorCode errorCode) {
		DataResponse dataResponse = new DataResponse();
		dataResponse.setCode(errorCode.code);
		dataResponse.setMessage(errorCode.msg);
		return dataResponse;
	}

	/**
	 * 只允许无明显界定的业务错误场景使用
	 * @param msg 消息
	 * @return
	 */
	public static DataResponse builderFailed(String msg) {
		DataResponse dataResponse = new DataResponse();
		dataResponse.setCode(ErrorCode.business_error.code);
		dataResponse.setMessage(msg);
		return dataResponse;
	}
}
